package com.lkb.administrator.okuweather.util;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.lkb.administrator.okuweather.db.City;
import com.lkb.administrator.okuweather.db.County;
import com.lkb.administrator.okuweather.db.Province;
import com.lkb.administrator.okuweather.gson.Weather;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2018/6/1.
 */

public class UtilIty {


    public static boolean handleProviceResponce(String response) {

        if (!TextUtils.isEmpty(response)) {

            try {
                JSONArray allProvices = new JSONArray(response);
                for (int i = 0; i < allProvices.length(); i++) {

                    JSONObject proviceObjce = allProvices.getJSONObject(i);
                    Province abc = new Province();
                    abc.setProviceName(proviceObjce.getString("name"));
                    abc.setProviceCode(proviceObjce.getInt("id"));
                    abc.save();


                }


                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }

        return false;
    }




    public static boolean handleCityResponce(String response,int proviceId) {

        if (!TextUtils.isEmpty(response)) {

            try {
                JSONArray allCity = new JSONArray(response);
                for (int i = 0; i < allCity.length(); i++) {

                    JSONObject proviceObjce = allCity.getJSONObject(i);
                    City abc = new City();
                    abc.setCityName(proviceObjce.getString("name"));
                    abc.setCityCode(proviceObjce.getInt("id"));
                    abc.setProvinceCode(proviceId);
                    abc.save();


                }


                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }




        }

        return false;
    }



    public static boolean handleCountryResponce(String response,int cityId) {

        if (!TextUtils.isEmpty(response)) {

        try {
        JSONArray allCountry = new JSONArray(response);
        for (int i = 0; i < allCountry.length(); i++) {

        JSONObject proviceObjce = allCountry.getJSONObject(i);
        County abc = new County();
        abc.setCountyName(proviceObjce.getString("name"));
        abc.setWeatherId(proviceObjce.getString("weather_id"));
        abc.setCityId(cityId);
        abc.save();


        }


        return true;
        } catch (JSONException e) {
        e.printStackTrace();
        }




        }

        return false;
        }

    public static Weather handleResponse(String response){

        Log.d("ddd", response);

        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.getJSONArray("HeWeather");
            String weathContent = jsonArray.getJSONObject(0).toString();
            return  new Gson().fromJson(weathContent,Weather.class);



        } catch (JSONException e) {
            e.printStackTrace();
        }


        return  null;
    }









    }
